package view
{
	import assets.view.ListViewSymbol;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.events.Event;
	
	import model.vo.ViewVO;
	
	import view.components.TabButtonView;

	public class ListView extends ListViewSymbol
	{
		public static const NAME:String 				= "ListView";
		
		public static const CHANGED:String				= NAME + "Changed";
		public static const EXPANDED:String				= NAME + "Expanded";
		
		private var listViewData:Array;
		private var viewData:ViewVO;
		private var listViewWidth:Number;
		private var listViewHeight:Number;
		
		private var currentTabBtn:TabButtonView;
		private var listViewCurrentData:XML;
		
		private var numTabBtns:int;
		
		private var listYPos:Number;
		
		private var tabsPos:Vector.<Number>;
		
		private var tabBtnsArr:Array;
		
		public function ListView()
		{
		}
		
		public function init( data:Object, viewVO:ViewVO ):void
		{
			listViewData = data as Array;
			viewData = viewVO;
			listViewWidth = viewData.CONTENT_WIDTH;
			listViewHeight = viewData.CONTENT_HEIGHT - viewData.FOOTER_HEIGHT;

			numTabBtns = listViewData.length;
			
			buildListView();
		}
		
		public function show():void
		{
			FX.cascadeFadeIn( tabBtnsArr );
		}
		
		public function addContent( obj:Object ):void
		{
			var content:Object = obj;
			content.x = Math.round( contentContainer.width );
			
			contentContainer.addChild( content as DisplayObject) ;
		}
		
		private function buildListView():void
		{
			tabBtnsArr = new Array();
			
			var tabBtn:TabButtonView;
			
			var i:uint;
			for( i = 0; i < numTabBtns; i++ )
			{
				tabBtn = new TabButtonView();
				tabBtn.addEventListener( TabButtonView.CLICKED, tabBtnClickedHandler );
				tabBtn.addEventListener( TabButtonView.EXPANDING, tabExpandingHandler );
				tabBtn.addEventListener( TabButtonView.EXPANDED, tabExpandedHandler );
				tabBtn.init( listViewData[i], viewData, i );
				
				tabBtn.y = Math.round( itemsContainer.height );
				
				itemsContainer.addChild( tabBtn );
				tabBtnsArr.push( tabBtn );
			}
			
			paintBckgrd();
			paintMask();
			paintFrame();
			
			contentContainer.y = Math.round( bckgrdContainer.height );
			
//			itemsContainer.mask = maskContainer;
		}
		
		private function paintBckgrd():void
		{
			bckgrdContainer.alpha = 0;
			
			var g:Graphics = bckgrdContainer.graphics;
			g.clear();
			g.beginFill( viewData.CONTENT_BCKGRD_COLOR, 1 );
			g.drawRect( 0, 0, listViewWidth, listViewHeight );
			g.endFill();
			
			FX.fadeIn( bckgrdContainer );
		}
		
		private function paintMask():void
		{
			var g:Graphics = maskContainer.graphics;
			g.clear();
			g.beginFill( 0xff0000, 0.5 );
			g.drawRect( 0, 0, listViewWidth, listViewHeight );
			g.endFill();
		}
		
		private function paintFrame():void
		{
			var g:Graphics = frameContainer.graphics;
			g.clear();
			g.lineStyle( viewData.frameWeight, viewData.frameColor );
			g.drawRect( 0, 0, listViewWidth, listViewHeight );
		}
		
		private function tabBtnClickedHandler( e:Event ):void
		{			
			if( currentTabBtn && currentTabBtn != e.target as TabButtonView ) currentTabBtn.unselect();
			
			
			currentTabBtn = e.target as TabButtonView;
			
			if( !currentTabBtn.isExpanded )listYPos = 0;
			else listYPos = -Math.round( currentTabBtn.height * currentTabBtn.index );
			
			FX.translateY( itemsContainer, listYPos );
		}
		
		private function tabExpandedHandler( e:Event ):void
		{
			var prevBtn:TabButtonView;
			var btn:TabButtonView;
			var i:uint;
			for( i = currentTabBtn.index + 1; i < numTabBtns; i++ )
			{
				prevBtn = itemsContainer.getChildAt( i - 1 ) as TabButtonView;
				btn = itemsContainer.getChildAt( i ) as TabButtonView;
				btn. y = prevBtn.y + prevBtn.height;
			}
			dispatchEvent( new Event( EXPANDED ) );
		}
		
		private function tabExpandingHandler( e:Event ):void
		{
			var prevBtn:TabButtonView;
			var btn:TabButtonView;
			var i:uint;
			for( i = currentTabBtn.index + 1; i < numTabBtns; i++ )
			{
				prevBtn = itemsContainer.getChildAt( i - 1 ) as TabButtonView;
				btn = itemsContainer.getChildAt( i ) as TabButtonView;
				btn. y = prevBtn.y + prevBtn.height;
			}
		}
		
		public function get currentData():Object
		{
			return currentTabBtn.data;
		}
	}
}